package com.android.baseconfig.common.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.android.baseconfig.R;
import com.android.baseconfig.base.BaseConfigApplication;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: ToastUtils <br>
 * 作者: lei <br>
 * 日期: 2020/9/10 <br>
 */
public class ToastUtils {
    private static volatile ToastUtils sToastUtil = null;

    private Toast mToast = null;

    public static int TOAST_Y_OFFSET = 0;
    private static final long DROP_DUPLICATE_TOAST_TS = 2 * 1000; // 2s

    private static String sLast = "";

    private static long sLastTs = 0;

    private static Toast mBasicToast = null;
    private static TextView tvToast;

    /**
     * 获取实例
     *
     * @return
     */
    public static ToastUtils getInstance() {
        if (sToastUtil == null) {
            synchronized (ToastUtils.class) {
                if (sToastUtil == null) {
                    sToastUtil = new ToastUtils();
                }
            }
        }
        return sToastUtil;
    }

    protected Handler handler = new Handler(Looper.getMainLooper());

    /**
     * 显示Toast，多次调用此函数时，Toast显示的时间不会累计，并且显示内容为最后一次调用时传入的内容
     * 持续时间默认为short
     *
     * @param tips 要显示的内容
     *             {@link Toast#LENGTH_LONG}
     */
    public void showToast(Context context, final String tips) {
        showToast(context, tips, Toast.LENGTH_SHORT);
    }

    public void showToast(Context context, final int tips) {
        showToast(context, tips, Toast.LENGTH_SHORT);
    }

    /**
     * 显示Toast，多次调用此函数时，Toast显示的时间不会累计，并且显示内容为最后一次调用时传入的内容
     *
     * @param tips     要显示的内容
     * @param duration 持续时间，参见{@link Toast#LENGTH_SHORT}和
     *                 {@link Toast#LENGTH_LONG}
     */
    public void showToast(final Context context, final String tips, final int duration) {
        if (android.text.TextUtils.isEmpty(tips)) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(context, tips, duration);
                    mToast.show();
                } else {
                    mToast.setText(tips);
                    mToast.setDuration(duration);
                    mToast.show();
                }
            }
        });
    }

    public void showToast(final Context context, final int tips, final int duration) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(context, tips, duration);
                    mToast.show();
                } else {
                    mToast.setText(tips);
                    mToast.setDuration(duration);
                    mToast.show();
                }
            }
        });
    }

    public void showToast(final Context context, final String tips, final int duration,
                          final int yOffset) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast mToast = Toast.makeText(context, tips, duration);
                mToast.setGravity(mToast.getGravity(), 0, yOffset);
                mToast.show();
            }
        });
    }

    /**
     * 自定义toast
     *
     * @param str
     */
    public synchronized void showCustomToast(int resourceId) {
        showCustomToast(BaseConfigApplication.getInstance(), BaseConfigApplication.getInstance().getResources().getString(resourceId));
    }

    /**
     * 自定义toast
     *
     * @param str
     */
    public synchronized void showCustomToast(String str) {
        showCustomToast(BaseConfigApplication.getInstance(), str);
    }

    /**
     * 自定义toast
     *
     * @param context
     * @param str
     */
    public synchronized void showCustomToast(Context context, String str) {
        try {
            long newTs = System.currentTimeMillis();
            if (str != null
                    && (!str.equals(sLast) || newTs < sLastTs || (newTs - sLastTs) > DROP_DUPLICATE_TOAST_TS)) {
                sLast = str;
                sLastTs = newTs;
                TextView tvToast = null;
                if (mBasicToast == null) {
                    mBasicToast = new Toast(context);
                    View toastView = LayoutInflater.from(context).inflate(
                            R.layout.layout_custom_toast, null);
                    tvToast = (TextView) toastView.findViewById(R.id.tv_toast);
                    tvToast.setText(str);
                    mBasicToast.setView(toastView);
                    // int px = DensityUtils.dp2px(context, 60);
                    mBasicToast.setGravity(Gravity.CENTER, 0, 0);
                    mBasicToast.setDuration(Toast.LENGTH_SHORT);// 默认只显示2S
                } else {
                    tvToast = mBasicToast.getView().findViewById(R.id.tv_toast);
                    tvToast.setText(str);
                }
                mBasicToast.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
